﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

namespace Statistic
{
    public static class HtmlString
    {
        /**
 * Cleans htmlcode
 * @param url
 *  the html code to be cleaned
 * @param first
 *  from..
 * @param second
 *   to..
 * @return result_
 *   The html code cleaned
 */
        public static string CleanSource(string source, string first, string second, string type)
        {
            string cleanedSource = "";
            try
            {
                int firstChr = source.IndexOf(first, StringComparison.OrdinalIgnoreCase);
                int lastChr = source.IndexOf(second, StringComparison.OrdinalIgnoreCase);
                int length = lastChr - firstChr;
                string strTemp = source.Substring(firstChr, length);
                cleanedSource = strTemp;
                if (type == "sky")
                {
                    cleanedSource = strTemp.Remove(0, 6);
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                cleanedSource = "";
            }
            return cleanedSource;
        }

        /**
        * Converts date to the correct format
        * @param result
        * @return result
        *  correct date format
        */
        public static DateTime GetDate(string htmlCode, string message, string type)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("el-GR", true);
            string url = "";
            int FirstChr = htmlCode.IndexOf(message, StringComparison.OrdinalIgnoreCase);
            DateTime dateTime;
            //!!
            try
            {
                int FirstChr1 = htmlCode.Length - 1;
                int yo = FirstChr1 - FirstChr;
                string strTemp = htmlCode.Substring(FirstChr, yo);
                if (type == "kar")
                {
                    url = CleanSource(strTemp, "<div>", "</div>", "kar");
                    htmlCode = url.Remove(0, 5);
                }
                if (type == "sky")
                {
                    url = CleanSource(strTemp, "advertValues" + "\"" + ">", "</span>", "sky");
                    htmlCode = url.Remove(0, 9);
                }
                dateTime = DateTime.Parse(htmlCode, CultureInfo.CurrentCulture);
            }
            catch (ArgumentOutOfRangeException)
            {
                dateTime = DateTime.Now;
            }
            catch (FormatException)
            {
                dateTime = DateTime.Now;
            }
            return dateTime;
        }

        /**
         * Check if page exists
         * @param url
         *   html page
         * @return result_
         *   True if exists False if page doesn't exist
         */
        public static bool PageExists(string source, string message)
        {
            int FirstChr = source.IndexOf(message, StringComparison.CurrentCultureIgnoreCase);
            if (FirstChr == -1)
                return true;
            else
                return false;
        }

        /**
* Cleans source code from html tags
* @param source
*   source code to be cleaned
* @return
*   clened source code
*/
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }
    }
}